package edu.neepu;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class CreateFile extends Thread{
    public String fileName;
    public String imgUrl;
    public CreateFile(String fileName,String imgUrl){
        this.fileName = fileName;
        this.imgUrl = imgUrl;
    }
    public void run() {
        String path = "D:/img/"+fileName+".jpg";
        File dirName = new File(path);
        System.out.println(imgUrl);
        try {
            URL url = new URL(imgUrl);
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(10 * 1000);
            InputStream in = connection.getInputStream();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dirName));
            byte[] buf = new byte[1024];
            int size;
            while (-1 != (size = in.read(buf))) {
                out.write(buf, 0, size);
            }
            out.close();
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        synchronized (Config.indexLock) {
            if (Config.index>0){
                Config.index--;
            }
        }


    }
}
